package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Dep;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.Store;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.exception.AuthenticationException;
import cn.itcast.erp.service.StoreService;
import org.apache.commons.io.IOUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/*
 *@author 刘鑫鑫
 * @date 2019/6/5
 * */

@RestController
@RequestMapping("/store")
public class StoreController {

    @Resource
    private StoreService storeService;

    @CheckPower("仓库")
    @RequestMapping("/insert")
    public AjaxResult insertStore(@Validated Store store) {
        AjaxResult ar = new AjaxResult();

        int i = this.storeService.insertStore(store);
        if(i>0){
            ar.setMsg("添加成功");
        }else{
            ar.setMsg("添加失败");
        }

        return ar;
    }

    //修改仓库
    @CheckPower("仓库")
    @RequestMapping("/update")
    public AjaxResult updateStore(@Validated Store store) {

        AjaxResult ar = new AjaxResult();
        int i = this.storeService.updateStore(store);
        if (i>0){
            ar.setMsg("修改成功");
        }else {
            ar.setMsg("修改失败");
        }
        return ar;
    }

    //删除仓库
    @CheckPower("仓库")
    @RequestMapping("/delete")
    public AjaxResult deleteStore(Long uuid) {

        AjaxResult ar = new AjaxResult();

        int i = this.storeService.deleteStore(uuid);
        if (i > 0) {
            ar.setMsg("删除成功");
        } else {
            ar.setMsg("删除失败");
        }
        return ar;
    }

    //按条件查询仓库
    @CheckPower("仓库")
    @RequestMapping("/find")
    public AjaxResult selectConditionStore(Store store) {
        return this.storeService.selectCondition(store);
    }


    //查询所有仓库(不分页)
    @CheckPower("仓库")
    @RequestMapping("/findAllStore")
    public List <Store> findAllStore() {
        return this.storeService.findAllStore();
    }

    ;


    //查询by id
    @CheckPower("仓库")
    @RequestMapping("/findById")
    public Store findStoreById(Long uuid) {
        return this.storeService.findStoreById(uuid);
    }

    //导出Excel
    @CheckPower("仓库")
    @RequestMapping("/export")
    public void getAllStoreEx(HttpServletResponse response) {
        try {
            response.reset();
            response.setContentType("application/msexcel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("仓库信息.xls", "UTF-8"));
            InputStream inputStream = storeService.getAllStoreEx();
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}